import $service from '../../service.js';
var app = getApp();
Page({
  data: {
    showSelfLogin: false,
    isEsgin: $service.isEsgin,
    hospitalType: $service.hospitalType,
    hisType: $service.isHis,
    autoplay: true,
    interval: 5000,
    duration: 500,
    indicatorDots: false,
    doctorlist: [],
    isLoginshow: false,
    departmentList: [],
    allDepartment: {},
    bannerList: [],
    isFee: false,
    feeData: null,
    isFee: true,
    hospitalNameApp:"",  //医院名称
    userName: '',
  },
  onLoad(e) {
    if (e.inquiryType == 5) {
      wx.clearStorageSync()
      this.setData({
        showSelfLogin: true
      })
    }
    //加载本页面的tabBar样式
    app.editTabBar1();
  },
  onShow() {
      // 如果是医生则跳到医生首页
      if ($service.getStore('userInfo')) {
        let userInfo = JSON.parse($service.getStore('userInfo'))
        // if ($service.getEnv() != 'develop') {
          if (userInfo.type === 'D') {
            let doctorInfo =  JSON.parse($service.getStore('doctorInfo'))
            if (doctorInfo.isCertification == 0 && $service.isEsgin) {
              wx.clearStorageSync()
            } else {
              wx.redirectTo({
                url: '/pages/doctor_side/index/index',
              })
            }
          }
        // }
      }
    if (!$service.getStore('userInfo')) {
      this.setData({
        isLoginshow: true
      })
    } else {
      if (JSON.parse($service.getStore('userInfo')).type !== 'D') {
        this.socketInit();
        app.http.getUserInfo().then(res => {
          this.setData({
            userName: res.data.userNickname
          })
        })
      }
      this.selectComponent("#doctorList").getConsultationFee()
    }
    this.init();

    // wx.reLaunch({
    //   url: "/pages/chat/consult_chat/consult_chat"
    // })
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.init()
    if ($service.getStore('userInfo')) {
      this.selectComponent("#doctorList").getConsultationFee()
    }
    wx.stopPullDownRefresh();
  },
  init() {
    this.getBannerList();
    this.getDepartmentListPc();
    this.getHomeDoctorList();
    // this.getActivity();
    this.getConfigureName();
  },
  // 建立webSocket
  socketInit() {
    app.webSocket.initWebSocket({
      url: $service.wsUrl
    })
    console.log('会诊的')
    app.consultationWebsocket.initConsultationSocket({
      url: $service.consultationWsUrl
    })

    app.webSocket.onReceivedMsg(res => {
      if (res.packageType == 12) {
        this.getHomeDoctorList();
        this.selectComponent("#doctorList").getConsultationFee()
      }
    })
  },
  // 获取轮播图
  getBannerList() {
    app.http.getBannerList({}).then(res => {
      this.setData({
        bannerList: res.data.list
      })
    })
  },
  // 首页科室列表
  getDepartmentListPc() {
    app.http.getDepartmentListPc({}).then(res => {
      let list = [];
      res.data.list.forEach(e => {
        if (list.length < 7 ) {
          if ((e.id != 0 && !this.data.hisType) || (this.data.hisType && e.isDisplay == 1 && e.id != 0)) {
            list.push(e)
          }
        }
        if (e.id == 0) {
          this.data.allDepartment = e
        }
      })
      this.setData({
        departmentList: list,
        allDepartment: this.data.allDepartment
      })
    })
  },
  // 获取医生列表
  getHomeDoctorList() {
    let params = {
        "current": 1,
        "departmentId": 0,
        "inquiryType": "",
        "keyWord": "",
        "parametn": "",
        "size": 0
    }
    app.http.getHomeDoctorList({
      params
    }).then(res => {
      console.log(res)
      // let list = $service.deepClone(res.data.list)
      // list.forEach((item, index) => {
      //    if (item.isOnline === 1) {
      //       list.splice(index, 1);
      //       list.unshift(item)
      //    }
      // })
      this.setData({
        doctorlist: res.data.list
      })
    }).finally(() => {
      wx.stopPullDownRefresh()
    })
  },
  // 科室icon跳转
  toside(e) {
    if (this.data.isLoginshow == true) { //判断是否登录
      $service.loginuserid()
      return
    }
    var index = e.currentTarget.dataset.index;
    if (index == -1) {
      $service.openPage('../user_side/inquiry_service/inquiry_service')
    } else {
      $service.openPage('../user_side/inquiry_service/inquiry_service?departmentId=' + index)
    }
  },
  //监听input框，去搜索医生界面
  goToSelect(e) {
    console.log(1);
    $service.openPage('../user_side/index/index_search/index_search?doctor_name=' + e.detail.value)
  },
  //页面跳转
  gotopage(e) {
    if (this.data.isLoginshow == true) { //判断是否登录
      $service.loginuserid()
      return
    }
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.num;
    var type = e.currentTarget.dataset.type;
    if (index == 0) { //我的消息
      $service.openPage('../user_side/index/index_news/index_news')
    } else if (index == 2) { //我的处方
      $service.openPage('../user_side/personal/personal_prescription/personal_prescription')
    } else if (index == 3) { //网络问诊 核酸检查
      $service.openPage('/pages/prescription_flow/order_list/order_list')
    } else if (index == 4) {
      //获取体质辨识信息
      // app.http.getConstitution({}).then((res) => {
      //   if(res.data.isValid==1){
      //     $service.openPage('/pages/user_side/physique_test/test_camera/index')
      //   }
      //   else{
      //     $service.showToast('该功能暂未开通');
      //   }
      // })
      $service.openPage('/pages/user_side/inquiry_service/inquiry_service?type=4')
    } else if (index == 1) { //权威医生
      wx.showModal({
        title: '咨询提示',
        content: '请尽可能把病情一条信息完整描述：\r\n症状+体征+持续时间+起因+缓解情况+已用治疗+病史+既往查体结果和病历\r\n从而缩短医生在线询问和答复时间。医院目前调动在线询问医生有限，会尽快回复。',
        success(res) {
          if (res.confirm) {
            $service.openPage('../user_side/index/index_doctorlist/index_doctorlist');
          }
        }
      });
    } else if (index == 5) {
      $service.openPage('../user_side/personal/personal_interrogation/personal_interrogation')
    }
  },
  goLogin() {
    $service.openPage('/pages/login/login')
  },
  //获取活动专栏信息
  getActivity() {
    app.http.getActivity({}).then((res) => {
      console.log(res);
    })
  },
  //获取首页医院名称
  getConfigureName() {
    app.http.getConfigureName({}).then((res) => {
      this.setData({
        hospitalNameApp:res.data.hospitalNameApp
      })
      wx.setStorageSync('hospitalName', res.data.hospitalNameApp)
      wx.setNavigationBarTitle({
        title: res.data.hospitalNameApp,
        fail: (err) => {
          console.log(err);
        }
      })
    })
  },
  // 分享小程序
  onShareAppMessage(e) {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: this.data.hospitalNameApp
        })
      }, 1000)
    })
    return {
      title: this.data.hospitalNameApp,
      path: 'pages/user_index/user_index',
      promise
    }
  },
})
